package com.mycompany.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mycompany.entity.Number;
import com.mycompany.entity.Rule;
import com.mycompany.service.NumberService;
import com.mycompany.service.RuleService;
import com.mycompany.unity.NumberUnity;

/**
 * Handles requests for the application home page.
 */
@Controller
public class NumberController {
	
	private static final Logger logger = LoggerFactory.getLogger(NumberController.class);
	
	@Autowired
	NumberService numberService;
	

	@Autowired
	RuleService ruleService; 
	
	@RequestMapping(value = "/admin/number", method = RequestMethod.GET)
	public String numberListHandle(Model model) {
		logger.debug("Number List here!");
		List<Number> numbers = numberService.findAllTop10();
		model.addAttribute("numbers", numbers);
		return "admin/number/number_list";
	}
	@RequestMapping(value = "/admin/numberSearch")
	public String numberFindHandle(Model model,@RequestParam(value = "number") String number,@RequestParam(value = "type") String type) {
		logger.debug("numberFindHandle here!");
		Number u = new Number();
		u.setNumber(number);
		u.setType(type);
		List<Number> numbers = numberService.findListByCodition(u);
		model.addAttribute("numbers", numbers);
		return "admin/number/number_list";
	}
	@RequestMapping(value = "/admin/numberDelete")
	public String numberDeleteHandle(Model model,@RequestParam(value = "id") int id) {
		logger.debug("numberDeleteHandle here!");
		Number u = new Number();
		u.setNumberId(id);
		u  = numberService.findById(id);
		numberService.remove(u);
		List<Number> numbers = numberService.findAllTop10();
		model.addAttribute("numbers", numbers);
		return "admin/number/number_list";
	}
	
	@RequestMapping(value = "/admin/numberEditForm")
	public String numberEditFormHandle(Model model,@RequestParam(value = "id") int id) {
		logger.debug("numberEditFormHandle here!");
		Number u = new Number();
		u.setNumberId(id);
		u  = numberService.findById(id);
		model.addAttribute("number", u);
		model.addAttribute("action","numberEdit");
		return "admin/number/number_form";
	}
	
	@RequestMapping(value = "/admin/numberEdit")
	public String numberEditHandle(Model model,@ModelAttribute( "Number" ) @Valid Number number) {
		logger.debug("numberEditHandle here!");
		number.setActiveFlg(true);
		numberService.merge(number);
		List<Number> numbers = numberService.findListByCodition(number);
		model.addAttribute("numbers", numbers);
		return "admin/number/number_list";
	}
	
	@RequestMapping(value = "/admin/numberAdd")
	public String numberAddHandle(Model model,@ModelAttribute( "Number" ) @Valid Number number) {
		logger.debug("numberAdd here!");
		number.setActiveFlg(true);
		
		numberService.persist(number);
		
		List<Number> numbers = numberService.findAllTop10();
		model.addAttribute("numbers", numbers);
		return "admin/number/number_list";
	}
	
	
	@RequestMapping(value = "/admin/numberSummary", method = RequestMethod.GET)
	public String numberSummaryHandle(Model model) {
		logger.debug("numberSummaryHandle here!");
		
		Map<String,Integer> percen = NumberUnity.setPercen();
		
		//find all data before summary
		List<Number> numberAll = numberService.findAll();
		// get total price for all type
		Map<String,Integer> totals =  numberService.getNumberSummaryTotal(numberAll);
		// logic summary Number to List 
		Map<String,Map<String,Integer>> numbers = numberService.numberSummary(numberAll);
		
		// print summary data
		NumberUnity.printMap(numbers);
		
		// Get rule for budget
		List<Rule> ruleAll = ruleService.findAll();
		
		System.out.print(totals);
		
		model.addAttribute("totals", totals);
		model.addAttribute("numbers", numbers);
		model.addAttribute("percen", percen);
		model.addAttribute("rule", ruleAll.get(0));
		return "admin/number/number_summary";
	}
	
}
